package com.sc.nft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.ExchangeCdkRequest;
import com.sc.nft.entity.vo.CdkGasCostVO;
import com.sc.nft.entity.vo.ProductCdkRecordVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.CDKProductService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * 服务控制器
 *
 * @author Sun
 * @since 2022-05-30 13:42:06
 * @description 由 Mybatisplus Code Generator 创建
 *
 * @Slf4j
 * @RequiredArgsConstructor
 * @RestController
 * @RequestMapping("/user")
 * @Api(tags ="用户")
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/cdk")
@Api(tags ="兑换商品")
public class CDKProductController {

    private final CDKProductService cdkProductService;
    private final Redisson redisson;

    @ApiOperation(value = "兑换商品", notes = "兑换商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/exchangeCdk", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result exchangeCdk(@Valid @RequestBody ExchangeCdkRequest request) throws GlobalException {
        RLock lock = redisson.getLock(RedisKeyEnum.STOCK_LOCK.getKey()+request.getCdk());
        try{
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                cdkProductService.exchangeCdk(request.getCdk(), UserPool.getUser());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            return Result.fail("当前人数过多，请稍后再试");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }
    
    @ApiOperation(value = "获取上链费", notes = "获取上链费", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getGasCost", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<CdkGasCostVO> getGasCost(@Valid @RequestBody ExchangeCdkRequest request)  {
        return Result.success(cdkProductService.getGasCost(request.getCdk(),UserPool.getUserId()));
    }

    @ApiOperation(value = "兑换记录", notes = "兑换记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getExchangeList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<ProductCdkRecordVO> getExchangeList(@Valid @RequestBody PageRequest request)  {
        Page<ProductCdkRecordVO> page= cdkProductService.getExchangeList(request.getPageNo(), request.getPageSize());
        return PageResult.success(page);
    }



}
